python中的[nm]

python中的[n::m]

前一阵子读代码的时候发现了这样的写法,大概自己试了一下,n表示起始坐标,m表示间隔,举个例子就知道了

1
2
import numpy as np
a = np.array([1,2,3,4,5,6,7,8])

栗子

1
2
3
print(a[0::2])		# 输出起始坐标为0,间隔为2
print(a[1::2]) # 输出起始坐标为1,间隔为2
print(a[0::4]) # 输出起始坐标为0,间隔为4

输出结果:

1
2
3
[1 3 5 7]
[2 4 6 8]
[1 5]